import { defineStore } from 'pinia'

import { storesKey } from '@/config'

// 匹配规则相关默认配置
interface IMatchConfig {
  countryList: LabelValue<string>[]
  continentList: LabelValue<string>[]
  supportMethod: LabelValue<string>[]
  supportHttpVersion: LabelValue<string>[]
}

export const useConfigStore = defineStore(
  storesKey.config,
  () => {
    const apiUrl = ref('')
    function setApiUrl(url: string) {
      apiUrl.value = url
    }

    // 站点信息
    const websiteConfig = reactive({
      title: '',
      logo: '',
      official_website: '',
      enable_captcha: true,
      icon: ''
    })
    // async function getWebConfigInLogin() {
    //   const res = await websiteConfigApiInLogin()
    //   websiteConfig.title = res.website?.name || import.meta.env.VITE_APP_TITLE
    //   websiteConfig.enable_captcha = is(res.website?.captcha_admin_open)
    //   websiteConfig.official_website = res.website?.official_website || ''
    //   if (res.website?.logos?.length === 0) {
    //     websiteConfig.logo = getAssetsUrl('logo.png')
    //     websiteConfig.icon = getAssetsUrl('icon.png')
    //   } else {
    //     if (res.website?.use! < 0) {
    //       websiteConfig.logo = getAssetsUrl('logo.png')
    //       websiteConfig.icon = getAssetsUrl('icon.png')
    //     } else {
    //       const index = res.website?.use!
    //       websiteConfig.logo = res.website?.logos![index]!
    //       websiteConfig.icon = res.website?.logos![index]!
    //     }
    //   }
    //   localStorage.setItem('logo', websiteConfig.icon)
    // }
    // async function getWebConfig() {
    //   const res = await websiteConfigApi({ type: '1' })
    //   websiteConfig.title = res.website?.name || import.meta.env.VITE_APP_TITLE
    //   websiteConfig.enable_captcha = is(res.website?.captcha_admin_open)
    //   websiteConfig.official_website = res.website?.official_website || ''
    //   if (res.website?.logos?.length === 0) {
    //     websiteConfig.logo = getAssetsUrl('logo.png')
    //     websiteConfig.icon = getAssetsUrl('icon.png')
    //   } else {
    //     if (res.website?.use! < 0) {
    //       websiteConfig.logo = getAssetsUrl('logo.png')
    //       websiteConfig.icon = getAssetsUrl('icon.png')
    //     } else {
    //       const index = res.website?.use!
    //       websiteConfig.logo = res.website?.logos![index]!
    //       websiteConfig.icon = res.website?.logos![index]!
    //     }
    //   }
    //   localStorage.setItem('logo', websiteConfig.icon)
    // }

    // 匹配规则相关配置
    const matchConfig = reactive<IMatchConfig>({
      countryList: [],
      continentList: [],
      supportMethod: [],
      supportHttpVersion: []
    })
    // 获取匹配规则配置
    // async function getMatchConfig() {
    //   bootstrapConfigApi().then(res => {
    //     matchConfig.countryList = res.country?.map(item => {
    //       return { label: item.name!, value: item.code! }
    //     })!
    //     matchConfig.continentList = res.continent?.map(item => {
    //       return { label: item.name!, value: item.code! }
    //     })!
    //     matchConfig.supportMethod = res.request_method?.map(item => {
    //       return { label: item.toUpperCase(), value: item.toUpperCase() }
    //     })!
    //     matchConfig.supportHttpVersion = res.http_version?.map(item => {
    //       return { label: item.toUpperCase(), value: item.toUpperCase() }
    //     })!
    //   })
    // }

    return {
      apiUrl,
      setApiUrl,
      websiteConfig,
      // getWebConfig,
      // getWebConfigInLogin,
      matchConfig
      // getMatchConfig
    }
  },
  {
    persist: {
      storage: localStorage
    }
  }
)
